package com.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2016/12/5.
 */
public class StringUtils {
    public static boolean isEmpty(String str){
        return str==null||str.equals("");
    }
public static final String UTF8 = "UTF-8";
	
	public static final boolean isNullOrEmpty(String value){
		return value == null || value.length() == 0;
	}
	
	public static final String join(int[] val){
		return join(val,",");
	}
	public static final String join(int[] val,String separator){
		if(val == null || val.length == 0) return null;
		StringBuffer str = new StringBuffer(512);
		str.append(val[0]);
		for(int i=1;i<val.length;i++) 
			str.append(separator).append(val[i]);
		return str.toString();
	}
	
	public static final String dateFormat(Date date,String format){
		DateFormat df = new SimpleDateFormat(format);	
		return df.format(date);
	}
	
	public static final String hexString(byte[] bytes){
		if(bytes == null || bytes.length == 0) return "";
		StringBuffer hs = new StringBuffer(bytes.length * 2);
		for(int i=0;i<bytes.length;i++){
			String s = Integer.toHexString(bytes[i] & 0xFF);
			if(s.length() < 2) hs.append(0);
			hs.append(s);
		}
		return hs.toString();
	}
	
	public static final String toSHA1(String value) {
		try {
			MessageDigest digest = MessageDigest.getInstance("SHA-1");
			digest.update(value.getBytes());
			return hexString(digest.digest());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static final String toMD5(String value) {
		try {
			byte[] b = MessageDigest.getInstance("MD5").digest(value.getBytes(UTF8));
			return hexString(b);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}
}
